﻿using Kalman;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wechat.TenpayV3;

namespace QuDao
{
    public class PaymentBO : SingletonBase<PaymentBO>
    {
        readonly AppSettings appSettings;
        public PaymentBO() : base()
        {
            appSettings = AppConfig.Instance.GetSection<AppSettings>("AppSettings");
        }

        ///// <summary>
        ///// 生成JSAPI支付参数数据
        ///// </summary>
        ///// <param name="appid">微信小程序应用ID</param>
        ///// <param name="openid"></param>
        ///// <param name="orderID">订单ID</param>
        ///// <returns></returns>
        //public IDictionary<string, string> GeneratePayParameters(string appid, string openid, long orderID)
        //{
        //    var app = WxAppBO.Instance.GetItem(appid) ?? throw new AppException($"无效的应用：{appid}");
        //    var order = OrderBO.Instance.GetItem(orderID);
        //    if (order == null) throw new ApiException(StatusCode.C404, $"订单不存在：{orderID}");
        //    if (order.Status != 1) throw new AppException($"交易已完成：{orderID}");

        //    //var total_fee = 10;  //租户为调试模式时付一毛钱
        //    var amount = Math.Abs(order.OrderAmount); //注意负数
        //    var out_trade_no = orderID.ToString();
        //    var body = order.GoodsName;

        //    //if (!tenant.IsDebug)
        //    //{
        //    //    total_fee = int.Parse(decimal.Round(amount * 100, 0).ToString()); //支付总金额,以分为单位(amount * 100).ToString()
        //    //}
        //    var total_fee = int.Parse(decimal.Round(amount * 100, 0).ToString()); //支付总金额,以分为单位(amount * 100).ToString()

        //    var dic = TenpayClient.Instance.GeneratePayParameters(app, openid, out_trade_no, total_fee, body);
        //    return dic;
        //}

        ///// <summary>
        ///// 处理支付通知
        ///// </summary>
        ///// <param name="data">交易数据</param>
        ///// <returns></returns>
        //public bool PayNotify(TransactionData data)
        //{
        //    var amount = data.Total;
        //    var orderNo = data.OutTradeNo;
        //    var tradeNo = data.TransactionID;
        //    //if (!char.IsDigit(orderNo[0])) orderNo = orderNo.Remove(0, 1);

        //    try
        //    {
        //        var orderID = long.Parse(orderNo); //交易订单ID
        //        var order = OrderBO.Instance.GetItem(orderID);
        //        if (order == null) throw new AppException("交易订单不存在", "OK");//异常错误码设置为OK时，异常处理后需要向支付网关返回成功标志
        //        if (order.Status == 2) throw new AppException("该笔交易已经完成", "OK");
        //        //if (order.Status != (int)OrderStatus.待支付) throw new AppException("订单非待支付状态，可能已被取消", "OK");

        //        //var tenant = TenantBO.Instance.GetItem(order.TID);
        //        //if (!tenant.IsDebug)
        //        //{
        //        //    if (order.Amount * 100 != amount) throw new AppException("订单交易金额不一致", "OK");
        //        //}

        //        order.TradeNo = tradeNo;
        //        order.PayTime = data.SuccessTime;

        //        OrderBO.Instance.ConfirmPay(order);
        //    }
        //    catch (AppException ex)
        //    {
        //        if (ex.ErrorCode != "OK") return false;
        //    }

        //    return true;
        //}

        /// <summary>
        /// 处理退款通知
        /// </summary>
        /// <param name="data">退款数据</param>
        public void RefundNotify(RefundData data)
        {

        }
    }
}
